var postsData=require('../../../data/posts-data.js')
var app=getApp();

Page({
  data:{
    postData:{},
    currentPostId:-1,
    collected:false,
    isPlayingMusic:false
  },

  onLoad:function(option){

    var postId=option.id;
   
    console.log('detailPostId',postId);

    this.data.currentPostId=postId;
    var postData=postsData.postList[postId];

    this.setData({
      postData:postData
    })
    

    this.setCollectedState(postId);

    if(app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId
    ===postId){
      this.setData({
        isPlayingMusic:true
      })
    }
    
    this.setAudioMonitor();
  },

  //设置收藏状态
  setCollectedState:function(postId){
    //这里进行收藏状态的初始化
    var postsCollected = wx.getStorageSync('posts_collected');
    if (postsCollected) {
      var postCollected = postsCollected[postId];
      this.setData({
        collected: postCollected
      })
    } else {
      var postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync('posts_collected', postsCollected)
    }

  },


  //设置audio监听
  setAudioMonitor:function(){
    var that = this;
    //监听bgm播放函数
    wx.onBackgroundAudioPlay(function () {
      that.setData({
        isPlayingMusic: true
      })
      app.globalData.g_isPlayingMusic=true;
      app.globalData.g_currentMusicPostId = that.data.currentPostId;
    })

    //监听bgm暂停函数
    wx.onBackgroundAudioPause(function () {
      that.setData({
        isPlayingMusic: false
      })
      app.globalData.g_isPlayingMusic=false;
      app.globalData.g_currentMusicPostId=null;
    })
  },


  //切换收藏状态
  onColletionTap:function(event) {
    var postsCollected=wx.getStorageSync('posts_collected');
    var postCollected=postsCollected[this.data.currentPostId];
    postCollected=!postCollected;
    postsCollected[this.data.currentPostId]=postCollected;
    wx.setStorageSync('posts_collected', postsCollected);

    this.setData({
      collected:postCollected
    })


    wx.showToast({
      title:postCollected?"收藏成功":"取消成功"
    })
  },


  //分享按钮
  onShareTap:function(event){
    var itemList=[
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博"
    ]
    wx.showActionSheet({
      itemList:itemList,
      itemColor:"#405f80",
      success:function(res){
        console.log(res)
        //res.cancel 用户是不是点击了取消按钮
        //res.tapIndex 数组元素的序号，从0开始
        wx.showModal({
          title:"用户"+itemList[res.tapIndex],
          content:"用户是否取消,现在无法实现分享功能"
        })
      }
    })
  },
  
  //音乐播放
  onMusicTap:function(event){
    var currentPostId = this.data.currentPostId;
    var postData = postsData.postList[currentPostId];
    var isPlayingMusic=this.data.isPlayingMusic;
    if(isPlayingMusic){
      wx.pauseBackgroundAudio();
      this.setData({
        isPlayingMusic:false
      })
      
    }else{
      
      wx.playBackgroundAudio({
        dataUrl: postData.music.url,
        title: postData.music.title,
        coverImg:postData.music.coverImg
      })
      this.setData({
        isPlayingMusic: true
      })
      
    }

    


  
  }

  
})